package com.lex.crossgate.admin.modules.sys.controller;

import com.alibaba.fastjson.JSONObject;
import com.lex.crossgate.admin.common.log.config.Log;
import com.lex.crossgate.admin.common.support.shiro.MD5Utils;
import com.lex.crossgate.common.constants.Constant;
import com.lex.crossgate.admin.common.utils.JsonDataFormate;
import com.lex.crossgate.common.constants.R;;
import com.lex.crossgate.admin.modules.sys.entity.SysUser;
import com.lex.crossgate.admin.modules.sys.entity.SysUserRole;
import com.lex.crossgate.admin.modules.sys.service.SysUserRoleService;
import com.lex.crossgate.admin.modules.sys.service.SysUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.List;

/**
 * 系统用户管理
 * @ProjectName: crossgate
 * @Package: com.lex.crossgate.admin.modules.sys.controller
 * @ClassName: ${TYPE_NAME}
 * @Description: java类作用描述
 * @Author: 李贝留斯
 * @CreateDate: 2018/5/2 15:52
 * @Version: 1.0
 **/
@Api("用户管理API")
@Controller
@RequestMapping("sys/user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysUserRoleService sysUserRoleService;
    /**
     * @description: 用户列表
     * @author 李贝留斯
     * @email sunyadongsyd@163.com
     * @date 2018/5/12 21:04
     */
    @ApiOperation("跳转至新增页")
    @GetMapping(value = "/index.html")
    @RequiresPermissions("sys:user:user")
    public String index(){
        return "base/user/userList";
    }
    /**
     * @description: 跳转新增页
     * @author 李贝留斯
     * @email sunyadongsyd@163.com
     * @date 2018/5/16 21:09
     */
    @RequiresPermissions("sys:user:add")
    @RequestMapping(value = "/add.html")
    public ModelAndView add(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("base/user/userAdd");
        return modelAndView;
    }

    @RequiresPermissions("sys:user:edit")
    @RequestMapping(value = "/edit.html")
    public ModelAndView edit(String userId){
        ModelAndView modelAndView = new ModelAndView();
        SysUser sysUser = sysUserService.selectById(userId);
        modelAndView.setViewName("base/user/userEdit");
        modelAndView.addObject("sysUser",sysUser);
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping(value = "/configure.html")
    public ModelAndView configure(String userId){
        ModelAndView modelAndView = new ModelAndView();
        List<SysUserRole> list = sysUserRoleService.getUserRoleById(userId);
        String ids = null;
        for (SysUserRole sysUserRole : list){
            if (StringUtils.isEmpty(ids)){
                ids = String.valueOf(sysUserRole.getRoleId());
            }else {
                ids =ids+","+ sysUserRole.getRoleId();
            }
        }
        modelAndView.setViewName("base/user/userRoleConfig");
        modelAndView.addObject("userId", userId);
        modelAndView.addObject("ids", ids);
        return modelAndView;
    }




    @Log("保存用户")
    @RequiresPermissions("sys:user:add")
    @RequestMapping(value = "/save.html")
    @ResponseBody
    public R save(SysUser sysUser){
        sysUser.setPassWord(MD5Utils.encrypt(sysUser.getUserName(), sysUser.getPassWord() ));
        //sysUser.setUserId(4L);
        if(sysUserService.insert(sysUser)){
            return R.ok(Constant.SYSUSER);
        }else {
            return R.error(Constant.ERROR);
        }
    }

    @Log("删除用户")
    @RequestMapping(value = "/delete.html")
    @ResponseBody
    public R delete(String[] ids){
        if(sysUserService.deleteBatchIds(Arrays.asList(ids))){
            return R.ok(Constant.SYSUSER);
        }else {
            return R.error(Constant.ERROR);
        }
    }

    @Log("更新用户")
    @RequestMapping(value = "/update.html")
    @ResponseBody
    public R update(SysUser sysUser){
        if(sysUserService.updateById(sysUser)){
            return R.ok(Constant.SYSUSER);
        }else {
            return R.error(Constant.ERROR);
        }
    }

    @Log("查询用户")
    @ResponseBody
    @RequestMapping(value = "/getSysUser.html")
    public JSONObject getSysUser(int page,int limit,String userName,String mobile){
        PageHelper.startPage(page,limit);
        List<SysUser> userList = sysUserService.getSysUser(userName,mobile);
        PageInfo pageInfo = new PageInfo(userList);
        JSONObject jsonObject = JsonDataFormate.piecing(userList,pageInfo);
        return jsonObject;
    }

    @Log("保存权限")
    @RequestMapping(value = "/saveUserRole.html")
    @ResponseBody
    public R saveUserRole(String userId,String roleId){
        R r = sysUserRoleService.save(userId,roleId);
        return r;
    }

    /**
     * TODO: 前端验证用户名是否重复方法
     * @author 李贝留斯
     * @date 2018/5/18 16:08
     */
    @RequestMapping(value = "/getUserNumberByUserName.html")
    @ResponseBody
    public int getUserNumberByUserName(String userName){
        return sysUserService.getUserByUserName(userName);
    }
}
